library core;

use java.lang.System as System;
use java.lang.String as String;
use java.lang.Integer as Integer;
use java.io.File as File;
use java.util.regex.Pattern as Pattern;
use vh.sphynx.runtime.SphynxRuntime as Runtime;
use "vh.sphynx.runtime.pattern.DataPartMatchInfo" as DataPartMatchInfo;
use "vh.sphynx.runtime.pattern.CompositePatternMatchInfo" as CompositePatternMatchInfo;
use vh.sphynx.runtime.util.Folder as Folder;
use vh.sphynx.runtime.util.OutFile as OutFile;
use vh.sphynx.runtime.util.CsvFile as CsvFile;
use vh.sphynx.runtime.util.LogUtil as LogUtil;
use vh.sphynx.jobengine.SphynxScriptJob as SphynxScriptJob;
use vh.sphynx.jobengine.JobExecutionEngine as JobEngine;

use vh.sphynx.util.CharSequenceStream as SequenceStream;

const INFINITY = 9999999;
const NA = Runtime.NA;

function length(s) Runtime.length(s)

function exec(command, outputFile='') Runtime.exec(command, outputFile)

function print(message, ...) 
	System.out.printf(message.toString(), __args)
		
function println(message, ...) 
	System.out.println(String.format(message.toString(), __args))

function format(text, ...)
	String.format(text.toString(), __args)
	
namespace Pattern {

	define pattern DataPart class DataPartMatchInfo arguments dataPattern = "", 
							flag=MULTILINES|DOTALL, input = null
							//minOccur=1, maxOccur=1, returnLastMatch=true, 
							//position=0, updateParentPosition=true,
							//closeInput=0-1, mismatchedException=true;
	define pattern Composite class CompositePatternMatchInfo arguments input = null;

	var DefaultPatternCLass = null;

	const LOCATOR_BEGIN = 1;
	const LOCATOR_END = 2;

	const UNIX_LINES = 1;
	const CASE_INSENSTIVE = 2;
	const COMMENTS = 4;
	const MULTILINES = 8;
	const LITERAL = 16;
	const DOTALL = 32;
	const UNICODE_CASE = 64;
	const CANNON_EQ = 128;

	const LOCATOR_EQUAL = 1;
	const LOCATOR_CLOSE_TAG = 2;
	const LOCATOR_MATCH = 3;
	
	const LOCATOR_EXCLUDE_MATCH_TEXT = 1;

	function locate(patternText, position=LOCATOR_BEGIN, flag = MULTILINES|DOTALL, operator = LOCATOR_MATCH, optional=false, 
					excludeMatchedText=false, throwException=true, lastPosOnUnmatch=false, groups=null, matchInfoObject = this) 
		matchInfoObject.locate(position, patternText, flag, operator, lastPosOnUnmatch, optional, excludeMatchedText, throwException, groups)

	function locate_end(patternText, position=LOCATOR_END, flag = MULTILINES|DOTALL, operator = LOCATOR_MATCH, optional=false, 
					excludeMatchedText=false, throwException=true, lastPosOnUnmatch=false, groups=null, matchInfoObject = this) 
		matchInfoObject.locate(position, patternText, flag, operator, lastPosOnUnmatch, optional, excludeMatchedText, throwException, groups)
		
	function match(text, patternText, flag = MULTILINES)
		Runtime.compilePattern(patternText, flag).matcher(text)
		
	function isMatch(text, patternText, flag = MULTILINES)
		Runtime.compilePattern(patternText, flag).matcher(text).find()
	
	function isMatchFail(result) Runtime.isMatchFail(result)
	
	function ifPatternMatchFail(result, falseValue)
		if (Runtime.isMatchFail(result)) falseValue else result
	
	function matchPattern(patternText='', ...) {
		var temp = pattern<DataPart>(dataPattern = patternText, ...);
		
	}
}
	
namespace JobEngine {
	var DefaultJobEngine = new JobEngine("", null);
	
	function queueJob(func, engine = DefaultJobEngine, ...)
		engine.addJob(SphynxScriptJob.createJob(__interpreter, __context, func))
		
	function addWorker(engine = DefaultJobEngine)
		engine.addWorker()
	
	function waitForQueueDone(numberWorder=1, engine = DefaultJobEngine) {
		for(var i = 1; i <= numberWorder; i++)
			engine.addWorker();
		engine.waitForDone();
	}
}
	
namespace Logging {

	const INFO = 0;
	const WARN = 1;
	const ERROR = 2;

	var logger = new LogUtil();

	function configureLog(logFile, logPattern="%-5p: %8r(%d) [%t] - %m%n", consolePattern="%-5p: %m%n", append = false)
		logger.configure(map[
			"log4j.rootLogger" = "INFO, console, file",
			"log4j.appender.console" = "org.apache.log4j.ConsoleAppender",
			"log4j.appender.console.layout" = "org.apache.log4j.PatternLayout",
			"log4j.appender.console.layout.ConversionPattern" = consolePattern,
			"log4j.appender.file" = "org.apache.log4j.FileAppender", // RollingFileAppender
			"log4j.appender.file.File" = logFile,
			"log4j.appender.file.Append" = append,
			"log4j.appender.file.layout" = "org.apache.log4j.PatternLayout",
			"log4j.appender.file.layout.ConversionPattern" = logPattern
		])
	
	function log(message, ...)
		logger.log(INFO, message, __args)
	
	function log_error(message, ...)
		logger.log(ERROR, message, __args)
		
	function log_info(message, ...)
		logger.log(INFO, message, __args)
		
	function log_warning(message, ...)
		logger.log(ERROR, message, __args)
}